package org.wzy.spring.framework.beans;

import org.wzy.spring.framework.aop.JdkDynamicAopProxy;
import org.wzy.spring.framework.beans.factory.FactoryBean;
import org.wzy.spring.framework.beans.factory.config.BeanPostProcessor;
import org.wzy.spring.framework.transaction.config.AopAutoProxyConfigurer;

/**
 * 模拟 Spring BeanWrapperImpl 实现
 *
 * @author wzy_h
 * @version v_1.0
 * @date 2018/5/19 23:11
 * @since 1.8
 */
public class BeanWrapperImpl extends FactoryBean implements BeanWrapper {

    private BeanPostProcessor beanPostProcessor;

    private Object wrappedInstance;

    /**
     * JDK 动态代理实现, 用于为特定的类提供代理
     */
    private JdkDynamicAopProxy aopProxy = new JdkDynamicAopProxy();

    /**
     * 原始的对象通过反射 new 出来
     */
    private Object originalInstance;

    public BeanWrapperImpl(Object instance) {
        this.aopProxy.setTarget(instance);
        this.wrappedInstance = aopProxy.getProxy();
        this.originalInstance = instance;
    }

    @Override
    public Object getWrappedInstance() {
        return wrappedInstance;
    }

    @Override
    public Class<?> getWrappedClass() {
        return wrappedInstance.getClass();
    }

    public Object getOriginalInstance() {
        return originalInstance;
    }

    public BeanPostProcessor getBeanPostProcessor() {
        return beanPostProcessor;
    }

    public void setBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        this.beanPostProcessor = beanPostProcessor;
    }

    public void setAopAutoProxyConfigurer(AopAutoProxyConfigurer aopAutoProxyConfigurer) {
        this.aopProxy.setAopAutoProxyConfigurer(aopAutoProxyConfigurer);
    }
}
